package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.CmsChannelDao;
import cn.sumpu.app.guoguojie.domain.CmsChannel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class CmsChannelDaoImpl extends SqlSessionDaoSupport implements
		CmsChannelDao {

	private static Logger logger = LoggerFactory
			.getLogger(CmsChannelDaoImpl.class);

	// 添加频道
	@Override
	public void addCmsChannel(CmsChannel cmsChannel)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.addCmsChannel",
							cmsChannel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addCmsChannel database failure.", e);
			}
			throw new AppBusinessException("addCmsChannel database failure.", e);
		}
	}

	// 取得频道列表
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsChannel> getChannelList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<CmsChannel>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.getChannelList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelList database failure.", e);
			}
			throw new AppBusinessException("getChannelList database failure.",
					e);
		}
	}
	//删除频道
	@Override
	public void deleteChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.deleteChannel",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteChannel database failure.", e);
			}
			throw new AppBusinessException("deleteChannel database failure.", e);
		}
	}
	//根据频道Id取得频道信息
	@Override
	public CmsChannel getChannelById(String channelId)
			throws AppBusinessException {
		try {
			return  (CmsChannel) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.getChannelById",
							channelId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById database failure.", e);
			}
			throw new AppBusinessException("getChannelById database failure.",
					e);
		}
	}
	//根据父ID取得子节点的树结构
    @SuppressWarnings("unchecked")
    @Override
    public List<CmsChannel> getChildById(Map<String, Object> map) throws AppBusinessException {
        try {
            return  (List<CmsChannel>) getSqlSession()
                    .selectList(
                            "cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.getChildById",
                            map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getChannelById database failure.", e);
            }
            throw new AppBusinessException("getChannelById database failure.",
                    e);
        }
    }
    //更新频道
    @Override
    public void updateChannelById(CmsChannel cmsChannel) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CmsChannelMapper.updateChannelById", cmsChannel);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateChannelById database failure.", e);
            }
            throw new AppBusinessException("updateChannelById database failure.",
                    e);
        }
    }

}
